package com.multi.tool.guava.compare;

import com.alibaba.fastjson.JSON;
import lombok.Data;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class Test {

    public static void main(String[] args) {
        List<ProductSKU> productSKUList = new ArrayList<>();
        ProductSKU productSKU = new ProductSKU();
        productSKU.setScore(10);
        productSKU.setPurchasePrice(BigDecimal.TEN);
        productSKUList.add(productSKU);

        ProductSKU productSKU1 = new ProductSKU();
        productSKU1.setScore(20);
        productSKU1.setPurchasePrice(BigDecimal.ONE);
        productSKUList.add(productSKU1);

        ProductSKU productSKU2 = new ProductSKU();
        productSKU2.setScore(20);
        productSKU2.setPurchasePrice(BigDecimal.TEN);
        productSKUList.add(productSKU2);

        // 多条件比较器
        CompoundComparator<ProductSKU> compoundComparator = new CompoundComparator();

        // 从大到小
        compoundComparator.setComparator(Comparator.comparing(ProductSKU::getScore).reversed());

        // 从小到大
        compoundComparator.setComparator(Comparator.comparing(ProductSKU::getPurchasePrice));

        productSKUList.sort(compoundComparator);

        System.out.println(JSON.toJSON(productSKUList));
    }

    @Data
    static class ProductSKU {

        private float score = 0L;

        private BigDecimal purchasePrice;
    }
}
